import { Vector3, Ray3 } from "@script/index";

export default class Camera {
  private readonly eye: Vector3;
  private readonly front: Vector3;
  private readonly ref_up: Vector3;
  private readonly fov: number;
  private right: Vector3;
  private up: Vector3;
  private fov_scale: number;

  constructor(eye: Vector3, front: Vector3, ref_up: Vector3, fov: number) {
    this.eye = eye;
    this.front = front;
    this.ref_up = ref_up;
    this.fov = fov;
  }

  initialize(): void {
    this.right = this.front.cross(this.ref_up);
    this.up = this.right.cross(this.front);
    this.fov_scale = Math.tan(this.fov * 0.5 * Math.PI / 180) * 2;
  }

  generateRay(x: number, y: number): Ray3 {
    let r = this.right.multiply((x - 0.5) * this.fov_scale);
    let u = this.up.multiply((y - 0.5) * this.fov_scale);
    return new Ray3(this.eye, this.front.add(r).add(u).normalize());
  }

}